document.addEventListener('DOMContentLoaded', async () => {
  const config = await getStoredConfig();
  const imageArr = await getStoredImage();
  const tabInfo = await getStored(PAGE_KEY);
  const { imageSorting, fileName, } = config;
  const imageArrSort = imageArr.sort((a, b) => {
    if (imageSorting == '原网排序') {
      return a.index - b.index;
    }
    if (imageSorting == '大图优先') {
      return b.area - a.area;
    }
    if (imageSorting == '小图优先') {
      return a.area - b.area;
    }
  });
  const PAGE_TITLE = tabInfo.title.replace(/\s/g, '');
  const imageArrFinaly = imageArrSort.map((a, ai) => {
    const fileInfo = getFileInfo(a.src, {
      PAGE_TITLE,
      NO: ai + 1,
      DATE: new Date().getTime(),
    });
    a.fileName = formatFileName(fileName, fileInfo);
    return a;
  });
  console.log(imageArrFinaly);
  new Vue({
    el: '#app',
    data: {
      tabInfo,
      imageArr: imageArrFinaly,
    },
    methods: {
      handleDownImage() {
        imageArrFinaly.forEach(a => {
          downloadFile(a.src, a.fileName);
        });
        // 擦除浏览器的下载历史，不会在磁盘中删除文件
        chrome.downloads.erase({});
      },
    },
  })
});
